package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowFormHistoryService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.form.ExternalFormQueryDTO;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowFormRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFormExternalHistoryDO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowFormExternalHistoryDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * @author SiJiang
 * @createTime 2021/8/9 9:43 上午
 * @description
 */
@Service
public class FlowFormHistoryServiceImpl  implements FlowFormHistoryService {
    @Autowired
    @Lazy
    FlowFormRepository flowFormRepository;

    @Autowired
    private FlowTaskTraceRepository taskTraceService;

    @Autowired
    FlowFormExternalHistoryDAO flowFormExternalHistoryDAO;

    /**
     * 任务保存外置表单历史记录
     */
    @Override
    public void  recordTaskHistoryExternalForm(String taskId){
        //获取全局变量
        FlowFormExternalHistoryDO history = new FlowFormExternalHistoryDO();
        history.setVersionId(taskId);
        //获取此时的任务表单
        ExternalFormQueryDTO externalFormQueryDTO = new ExternalFormQueryDTO();
        externalFormQueryDTO.setTaskId(taskId);
        //如果此时的任务表单为外置表单
        if(flowFormRepository.isExternalForm(externalFormQueryDTO)){
            //解析任务表单渲染后url
            String taskFormUrl = flowFormRepository.filledExternalFormInfo(null,taskId).get("formUrl").asText();
            taskFormUrl = taskFormUrl + (taskFormUrl.contains("?")?"&":"?") + "versionId="+taskId;
            history.setTaskFormInstanceUrl(taskFormUrl);
        }
        //如果流程表单为外置表单
        String processInstanceId = taskTraceService.getTaskTraceByTaskId(taskId).getProcessInstanceId();
        externalFormQueryDTO.setTaskId(null);
        externalFormQueryDTO.setInstanceId(processInstanceId);
        if(flowFormRepository.isExternalForm(externalFormQueryDTO)){
            //解析任务渲染后url
            String  processFormUrl = flowFormRepository.filledExternalFormInfo(processInstanceId,null).get("formUrl").asText();
            processFormUrl = processFormUrl + (processFormUrl.contains("?")?"&":"?") + "versionId="+taskId;
            history.setProcessFormInstanceUrl(processFormUrl);
        }

        if(StringUtils.isEmpty(history.getProcessFormInstanceUrl())&&StringUtils.isEmpty(history.getTaskFormInstanceUrl())){
            return;
        }
        flowFormExternalHistoryDAO.insertExternalFormRecord(history);

    }

    /**
     * 开始节点保存流程表单历史记录
     * @param processInstanceId
     */
    @Override
    public void recordProcessHistoryExternalForm(String processInstanceId) {
        FlowFormExternalHistoryDO history = new FlowFormExternalHistoryDO();
        history.setVersionId(processInstanceId);
        ExternalFormQueryDTO externalFormQueryDTO = new ExternalFormQueryDTO();
        //如果流程表单为外置表单
        externalFormQueryDTO.setTaskId(null);
        externalFormQueryDTO.setInstanceId(processInstanceId);
        if(flowFormRepository.isExternalForm(externalFormQueryDTO)){
            //解析任务渲染后url
            String  processFormUrl = flowFormRepository.filledExternalFormInfo(processInstanceId,null).get("formUrl").asText();
            processFormUrl = processFormUrl + (processFormUrl.contains("?")?"&":"?") + "versionId="+processInstanceId;
            history.setProcessFormInstanceUrl(processFormUrl);
        }

        if(StringUtils.isEmpty(history.getProcessFormInstanceUrl())&&StringUtils.isEmpty(history.getTaskFormInstanceUrl())){
            return;
        }
        flowFormExternalHistoryDAO.insertExternalFormRecord(history);
    }
}
